const mongoose = require('mongoose');

const cardSchema = new mongoose.Schema({
  title: {
    type: String,
    required: true,
    trim: true
  },
  content: {
    type: String,
    required: true
  },
  chapter: {
    type: String,
    required: true
  },
  difficulty: {
    type: Number,
    required: true,
    min: 1,
    max: 5,
    default: 1
  },
  tags: [{
    type: String,
    trim: true
  }],
  relatedCards: [{
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Card'
  }],
  sourceDocument: {
    type: String,
    required: true
  },
  createdAt: {
    type: Date,
    default: Date.now
  },
  updatedAt: {
    type: Date,
    default: Date.now
  }
});

// 更新时间中间件
cardSchema.pre('save', function(next) {
  this.updatedAt = Date.now();
  next();
});

// 全文搜索索引
cardSchema.index({ title: 'text', content: 'text', tags: 'text' });

const Card = mongoose.model('Card', cardSchema);

module.exports = Card; 